--[[ Lambda RaidFrames
     Part of LambdaUI made by Gaius
	 Graphics are made by Gaius
	 ------------------------------
	 Contact me in irc://phcnet.de/#wow-interface
--]]

local filePath = "Interface\\Addons\\LambdaUI\\"

--[[  Lambda RaidBackground Class

--]]
local Lambda = {}

function Lambda:new(groupId)
	local Lambda = {}
    local filePath = filePath.."Generic\\"
	
    Lambda = CreateFrame("Button", "LambdaRaid"..groupId, UIParent)
   	
	Lambda.GroupHeader = Lambda:CreateFontString("LambdaRaid"..groupId.."Name","OVERLAY")
	
	Lambda:SetBackdrop({
		bgFile = "Interface\\Buttons\\WHITE8x8",
		edgeFile = filePath.."Border.tga",
		tile = true,
		tileSize = 32,
		edgeSize = 32,
		insets = {
			left = 32,
			right = 32,
			top = 32,
			bottom = 32,
		}
	})
	Lambda:SetBackdropColor(0,0,0,1)
	Lambda:SetBackdropBorderColor(0,0,0,1)
	
	Lambda:SetWidth(96)
	Lambda:SetHeight(165)
	
	Lambda.GroupHeader:SetFont(filePath.."NameFont.ttf",10)
	Lambda.GroupHeader:SetPoint("TOPLEFT",Lambda,"TOPLEFT",8,-8)
	Lambda.GroupHeader:SetPoint("TOPRIGHT",Lambda,"TOPRIGHT",-8,-8)
	
	Lambda:RegisterForDrag("RightButton")
	Lambda:SetMovable(true)
	Lambda:SetUserPlaced(true)
	
	Lambda:SetScript("OnDragStart", function()
		if IsAltKeyDown() then
			Lambda:StartMoving()
		end
	end)
	
	Lambda:SetScript("OnDragStop", function()
		Lambda:StopMovingOrSizing()
	end)
		
	return Lambda
end 

local Kappa = {}
function Kappa:new(RaidId, newHeight, parent)
	local Kappa = {}
	local filePath = filePath.."Generic\\"
	local TextColor = "|cFFFFFFFF"
	
	Kappa = CreateFrame("Button", "LambdaRaidUnit"..RaidId, UIParent, "SecureUnitButtonTemplate")
		
	Kappa.BackgroundTexture = Kappa:CreateTexture("LambdaRaidUnit"..RaidId.."OVERLAY","BACKGROUND")
	Kappa.HealthBar = CreateFrame("StatusBar","LambdaRaidUnit"..RaidId.."HealthBar", Kappa)
	Kappa.ManaBar = CreateFrame("StatusBar","LambdaRaidUnit"..RaidId.."ManaBar", Kappa)
	Kappa.Name = Kappa:CreateFontString("LambdaRaidUnit"..RaidId.."Name","OVERLAY")
	
	Kappa.BackgroundTexture:SetTexture(filePath.."RaidUnitBackground.tga")

	Kappa.BackgroundTexture:SetPoint("TOPLEFT",Kappa)
	Kappa.BackgroundTexture:SetTexCoord(0, 0.671875, 0, 0.78125)
	Kappa.BackgroundTexture:SetWidth(86)
	Kappa.BackgroundTexture:SetHeight(25)
		
	Kappa:SetAttribute("unit",RaidId)
	Kappa:SetAttribute("*type1","target")
	Kappa:SetAttribute("*type2","assist")
	
	Kappa:SetWidth(86)
	Kappa:SetHeight(25)
	
	Kappa.Name = Kappa:CreateFontString("LambdaRaidUnit"..RaidId.."Name","OVERLAY")
	Kappa.Name:SetFont("Interface\\Addons\\LambdaUI\\UnitFrames\\NameFont.ttf", 10)
	Kappa.Name:SetText("Bla")
	
	Kappa.Name:SetPoint("TOPLEFT",Kappa, "TOPLEFT", 2, -1)
	Kappa.Name:SetPoint("TOPRIGHT",Kappa, "TOPRIGHT", -2, -1)
	
	
	Kappa.HealthBar:SetStatusBarTexture(filePath.."RaidUnitStatusBar.tga")
	Kappa.HealthBar:SetWidth(82)
	Kappa.HealthBar:SetHeight(5)
	Kappa.HealthBar:SetPoint("TOPLEFT",Kappa, "TOPLEFT", 2, -11)
	
	Kappa.ManaBar:SetStatusBarTexture(filePath.."RaidUnitStatusBar.tga")
	Kappa.ManaBar:SetWidth(82)
	Kappa.ManaBar:SetHeight(5)
	Kappa.ManaBar:SetPoint("TOPLEFT",Kappa, "TOPLEFT", 2, -18)
	
	Kappa:SetScript("OnDragStart",function()
	if IsAltKeyDown() then
		parent:StartMoving();
	end
	end)
	
	Kappa:SetScript("OnDragStop",function()
	parent:StopMovingOrSizing();
	end)
	
	Kappa.update = function()
		if UnitName(RaidId) then -- has Unit
			
			local r = RAID_CLASS_COLORS[select(2,UnitClass(RaidId))].r;
			local g = RAID_CLASS_COLORS[select(2,UnitClass(RaidId))].g;
			local b = RAID_CLASS_COLORS[select(2,UnitClass(RaidId))].b;
			
			Kappa.Name:SetText(UnitName(RaidId))
			Kappa.Name:SetVertexColor(r,g,b)
			
			Kappa.HealthBar:SetStatusBarColor(0.964, 0.631, 0.102)
			Kappa.HealthBar:SetMinMaxValues(0, UnitHealthMax(RaidId))
			
			Kappa.ManaBar:SetMinMaxValues(0, UnitManaMax(RaidId))
			if UnitPowerType(RaidId) == 0 then -- mana color
				Kappa.ManaBar:SetStatusBarColor(0.2,0.6,1)
			elseif UnitPowerType(RaidId) == 1 then -- rage color
				Kappa.ManaBar:SetStatusBarColor(0.8,0.1, 0.1)
			elseif UnitPowerType(RaidId) == 2 then -- focus color O.o?
				Kappa.ManaBar:SetStatusBarColor(0.6, 0.3, 0)
			elseif UnitPowerType(RaidId) == 3 then -- Energy color
				Kappa.ManaBar:SetStatusBarColor(0.8, 0.9, 0)
			elseif UnitPowerType(RaidId) == 1 then -- Happiness
				Kappa.ManaBar:SetStatusBarColor(0.6, 0.5, 0)
			else
				Kappa.ManaBar:SetStatusBarColor(0.964, 0.631, 0.102)
			end
			
			Kappa.HealthBar:SetValue(UnitHealth(RaidId))
			Kappa.ManaBar:SetValue(UnitMana(RaidId))
		end
		if Kappa:IsShown() and parent:GetHeight() ~= newHeight then
			parent:SetHeight(newHeight)
			parent:Show()
		end
		if not Kappa:IsShown() then
			parent:Hide()
		end
	end
	
	Kappa:RegisterEvent("UNIT_MANA")	
	Kappa:RegisterEvent("UNIT_FOCUS")	
	Kappa:RegisterEvent("UNIT_ENERGY")	
	Kappa:RegisterEvent("UNIT_HEALTH")	
	Kappa:RegisterEvent("RAID_ROSTER_UPDATE")
	Kappa:RegisterEvent("PLAYER_ENTERING_WORLD")
	Kappa:SetScript("OnEvent",Kappa.update)	
	
	RegisterUnitWatch(Kappa)
	Kappa:SetFrameLevel(2)
	
	table.insert(LambdaObjectTable,Kappa:GetName())
	
	return Kappa
end

for i=0, 7 do

local RaidGroup = Lambda:new(1)

local Raid1 = Kappa:new("raid"..1+5*i, 58, RaidGroup)
Raid1:SetPoint("TOPLEFT",RaidGroup, "TOPLEFT", 5, -23)

local Raid2 = Kappa:new("raid"..2+5*i, 85, RaidGroup)
Raid2:SetPoint("TOPLEFT",RaidGroup, "TOPLEFT", 5, -50)

local Raid3 = Kappa:new("raid"..3+5*i, 113, RaidGroup)
Raid3:SetPoint("TOPLEFT",RaidGroup, "TOPLEFT", 5, -77)

local Raid4 = Kappa:new("raid"..4+5*i, 139, RaidGroup)
Raid4:SetPoint("TOPLEFT",RaidGroup, "TOPLEFT", 5, -104)

local Raid5 = Kappa:new("raid"..5+5*i, 165, RaidGroup)
Raid5:SetPoint("TOPLEFT",RaidGroup, "TOPLEFT", 5, -131)

if not RaidGroup:GetPoint() then
	RaidGroup:SetPoint("CENTER",UIParent)
end

end
